package cn.b.sky.mes.cm.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import cn.b.sky.mes.cm.dao.WorkUnitDao;
import cn.b.sky.mes.cm.dao.WorkUnitTypeDao;
import cn.b.sky.mes.cm.model.WorkUnit;
import cn.b.sky.mes.cm.model.WorkUnitJSON;
import cn.b.sky.mes.cm.model.WorkUnitTypeAssign;
import cn.b.sky.mes.cm.model.condition.WorkUnitCondition;
import cn.b.sky.mes.cm.service.WorkUnitService;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.utils.DatabaseUtils;
import cn.b.sky.i18n.utils.LocaleResourceBundle;
import cn.b.sky.user.SecurityContextUtil;

/**
 * <strong>Title : WorkUnitServiceImpl.java<br></strong>
 * <strong>Package : cn.b.sky.mes.cm.service.impl<br></strong>
 * <strong>Description : </strong>@工作单元服务类实现@<br> 
 * <strong>Create on : 2015年9月28日 上午11:25:36<br></strong>
 * <p>
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人 | 修改日期 | 修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
@Service("workUnitService")
public class WorkUnitServiceImpl implements WorkUnitService {

  private static final Logger LOG = LoggerFactory.getLogger(WorkUnitServiceImpl.class);

  @Resource
  WorkUnitDao workUnitDao;

  @Resource
  WorkUnitTypeDao workUnitTypeDao;

  @Override
  public List<WorkUnit> selectWorkUnitList(WorkUnitCondition workUnitCondition) {
    if (StringUtils.isBlank(workUnitCondition.getOrderField())) {
      workUnitCondition.setOrderField("CreateDateTime");
    }
    return workUnitDao.selectWorkUnitListForPaging(workUnitCondition);
  }

  @Override
  public int getWorkUnitCount(WorkUnitCondition workUnitCondition) {
    return workUnitDao.getWorkUnitCount(workUnitCondition);
  }

  @Override
  public WorkUnit getWorkUnit(String id, String siteCode) {

    return workUnitDao.getWorkUnit(id, siteCode);
  }

  @Override
  public void insertWorkUnit(WorkUnit workUnit) {
    workUnitDao.insertWorkUnit(workUnit);

  }

  @Override
  public void updateWorkUnit(WorkUnit workUnit) {
    workUnitDao.updateWorkUnit(workUnit);

  }

  @Override
  public BatchJobReport deleteWorkUnits(String id, String siteCode) {
    if (!StringUtils.isBlank(id)) {
      String message = id;
      String cause = "";
      boolean success = true;
      try {
        workUnitDao.deleteWorkUnits(id, siteCode);
      } catch (Exception e) {
        LOG.error("{}", e);
        success = false;
        String errorMsg = e.getMessage();
        if (DatabaseUtils.isConstraintsError(errorMsg)) {
          //外键删除已存在提示
          errorMsg = LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),
              SecurityContextUtil.getLanguageCode(), "dataBaseOperationPkFail.M")
              + LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),
                  SecurityContextUtil.getLanguageCode(), "notDelete.M");
        }
        cause = errorMsg;
      }
      return new BatchJobReport(message, cause, success);
    } else {
      return new BatchJobReport("", "id is null", false);
    }

  }

  /** (非 Javadoc) 
  * Title: getWorkUnitExactCount
  * Description:
  * @param workUnitCondition
  * @return
  
  */
  /**
   * <strong>Title : getWorkUnitExactCount<br></strong>
   * <strong>Description : </strong>@方法注释说明写在此处@<br> 
   * <strong>Create on : 2015年9月28日 下午7:09:28<br></strong>
   * <p>
   * @param workUnitCondition
   * @return
   * @see cn.b.sky.mes.cm.service.WorkUnitService#getWorkUnitExactCount(cn.b.sky.mes.cm.model.condition.WorkUnitCondition)
   * @throws 
   * @author dapeng.li<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  @Override
  public int getWorkUnitExactCount(WorkUnitCondition workUnitCondition) {
    return workUnitDao.getWorkUnitExactCount(workUnitCondition);
  }

  /** (非 Javadoc) 
  * Title: getWorkUnit
  * Description:
  * @param workUnitCondition
  * @return
  
  */
  /**
   * <strong>Title : getWorkUnit<br></strong>
   * <strong>Description : </strong><br> 
   * <strong>Create on : 2015年9月28日 下午7:16:07<br></strong>
   * <p>
   * @param workUnitCondition
   * @return
   * @see cn.b.sky.mes.cm.service.WorkUnitService#getWorkUnit(cn.b.sky.mes.cm.model.condition.WorkUnitCondition)
   * @throws 
   * @author dapeng.li<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  @Override
  public WorkUnit getWorkUnit(WorkUnitCondition workUnitCondition) {
    return workUnitDao.getWorkUnit(workUnitCondition);
  }

  /** (非 Javadoc) 
  * Title: insertWorkUnitTypeAssign
  * Description:
  * @param workUnit
  * @param workUnitTypeIds
  
  */
  /**
   * <strong>Title : insertWorkUnitTypeAssign<br></strong>
   * <strong>Description : </strong>插入unitTypeAssign表<br> 
   * <strong>Create on : 2015年9月29日 下午1:45:30<br></strong>
   * <p>
   * @param workUnit
   * @param workUnitTypeIds
   * @see cn.b.sky.mes.cm.service.WorkUnitService#insertWorkUnitTypeAssign(cn.b.sky.mes.cm.model.WorkUnit, java.lang.String[])
   * @throws 
   * @author dapeng.li<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  @Override
  public void insertWorkUnitTypeAssign(WorkUnit workUnit, String[] workUnitTypeIds, String siteCode) {
    WorkUnitTypeAssign assign = new WorkUnitTypeAssign();
    assign.setSiteCode(siteCode);
    assign.setWorkUnitCode(workUnit.getWorkUnitCode());

    for (String workUnitTypeId : workUnitTypeIds) {
      assign.setWorkUnitTypeCode(workUnitTypeId);
      workUnitTypeDao.insertWorkUnitTypeAssign(assign);
    }

  }

  /** (非 Javadoc) 
  * Title: updateWorkUnitTypeAssign
  * Description:
  * @param workUnit
  * @param workUnitTypeIds
  
  */
  /**
   * <strong>Title : updateWorkUnitTypeAssign<br></strong>
   * <strong>Description : </strong>插入unitTypeAssign表。先删后增<br> 
   * <strong>Create on : 2015年9月29日 下午1:49:24<br></strong>
   * <p>
   * @param workUnit
   * @param workUnitTypeIds
   * @see cn.b.sky.mes.cm.service.WorkUnitService#updateWorkUnitTypeAssign(cn.b.sky.mes.cm.model.WorkUnit, java.lang.String[])
   * @throws 
   * @author dapeng.li<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  @Override
  public void updateWorkUnitTypeAssign(WorkUnit workUnit, String[] workUnitTypeIds, String siteCode) {
    WorkUnitTypeAssign workUnitTypeAssign = new WorkUnitTypeAssign();
    workUnitTypeAssign.setSiteCode(siteCode);
    workUnitTypeAssign.setWorkUnitCode(workUnit.getWorkUnitCode());

    workUnitTypeDao.deleterAssignByUnitType(workUnitTypeAssign);
    insertWorkUnitTypeAssign(workUnit, workUnitTypeIds, siteCode);
  }

  @Override
  public List<WorkUnit> selectWorkUnitSuggest(WorkUnitCondition condition) {
    if (condition == null) {
      condition = new WorkUnitCondition();
    }
    if (StringUtils.isBlank(condition.getOrderField())) {
      condition.setOrderField("CreateDateTime");
    }
    return workUnitDao.selectWorkUnitSuggest(condition);
  }

  @Override
  public List<WorkUnitJSON> selectWorkUnitByCenterAndType(String siteCode, String workCenterCode, String workUnitType) {
    return workUnitDao.selectWorkUnitByCenterAndType(siteCode, workCenterCode, workUnitType);
  }

  @Override
  public List<WorkUnit> getAllWorkUnitListBySiteCode(String siteCode, Integer lockFlag) {
    return workUnitDao.getAllWorkUnitListBySiteCode(siteCode, lockFlag);
  }

}
